package cn.edu.qlnu.xky.test.controller;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

import cn.edu.qlnu.xky.test.common.MsgExchage;
import cn.edu.qlnu.xky.test.model.Student;
import cn.edu.qlnu.xky.test.model.User;
import cn.edu.qlnu.xky.test.service.StudentService;

/**
 * Servlet implementation class TestController
 * 接收从页面jsp传递来的参数，加工成对象或其他类型数据，作为参数传递给service，
 * 调用service的方法，获得结果，并将结果传递到jsp页面
 */
public class StudentController extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public StudentController() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		System.out.println("能否在后台看到我？我是StudentController的代码");
		request.setCharacterEncoding("utf-8");
		
		// 操作的标志，null是添加前的页面，add为添加，search为查询
		String flag = request.getParameter("flag");
		String url = "/student/add.jsp";
		System.out.println("flag=" + flag);
		if (flag == null) {
			url = "/student/add.jsp";
		} else {
			StudentService service = new StudentService();
			if (flag.equals("add")) {
				String psw = request.getParameter("psw");
				String gender = request.getParameter("gender");
				String userName = request.getParameter("userName");
				String birthday = request.getParameter("birthday");
				String[] hobbies = request.getParameterValues("hobby");
				String school = request.getParameter("school");
				String temp = request.getParameter("temp");
				// 应该封装这些信息，然后传递到后台类方法，存到数据库中
				Student stu = new Student();
				stu.setPsw(psw);
				stu.setGender(gender);
				stu.setUserName(userName);
				stu.setBirthday(MsgExchage.stringToDate(birthday));
				stu.setHobbies(hobbies);
				stu.setSchool(school);
				stu.setTemp(temp);

				int ret = service.addStudent(stu);
				String msg = "";
				if(ret >=1) {
					msg = "学生添加成功";
				}else if(ret==0){
					msg="学生添加失败";
				}else if(ret == -1) {
					msg = "学生添加操作出现异常，请咨询管理员";
				}
				request.setAttribute("msg", msg);
				url = "/student/search.jsp";
			} else if (flag.equals("search")) {
				String nameForSearch = request.getParameter("nameForSearch");
				if(nameForSearch == null) {
					nameForSearch="";
				}
				String pageNo = request.getParameter("pageNo");
				// 此处为查询模块
				request.setAttribute("page", service.searchByNamePage(nameForSearch,Integer.parseInt(pageNo)));
				url = "/student/search.jsp";
			} else if (flag.equals("edit")) {
				// 修改前的查询操作
				String id = request.getParameter("id");
				Student stu = service.searchById(Integer.parseInt(id));
				request.setAttribute("stu", stu);
				url = "/student/edit.jsp";
			}else if(flag.equals("doEdit")) {
				//真正的修改操作
				String id = request.getParameter("id");
				String gender = request.getParameter("gender");
				String userName = request.getParameter("userName");
				String birthday = request.getParameter("birthday");
				String[] hobbies = request.getParameterValues("hobby");
				String school = request.getParameter("school");
				String temp = request.getParameter("temp");
				
				Student stu = new Student();
				stu.setId(Integer.parseInt(id));
				stu.setGender(gender);
				stu.setUserName(userName);
				stu.setBirthday(MsgExchage.stringToDate(birthday));
				stu.setHobbies(hobbies);
				stu.setSchool(school);
				stu.setTemp(temp);
				
				
				int ret = service.editStudent(stu);
				String msg = "";
				if(ret == 0) {
					msg ="修改失败";
				}else {
					msg ="修改成功";
				}
				//TODO 后期要删除
				request.setAttribute("msg", msg);
				url = "/student/search.jsp";
			}else if(flag.equals("delete")) {
				//进行删除操作
				String id = request.getParameter("id");
				System.out.println("id="+id);
				int ret = service.deleteStudent(Integer.parseInt(id));
				String msg = "";
				if(ret == 0) {
					msg ="删除失败";
				}else if(ret >=1){
					msg ="删除成功";
				}else if(ret == -1) {
					msg = "学生删除操作出现异常，请咨询管理员";
				}
				request.setAttribute("msg", msg);
				url = "/student/search.jsp";
			}
		}
		request.getRequestDispatcher(url).forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
